package com.notary.email;

import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.util.Map;


@Service
public class MailServiceImpl implements MailService {

    //邮件发送者email
    @Value("${spring.mail.from}")
    private String from;
    //邮件系统发送方
    @Value("${spring.mail.systemSenderName}")
    private String systemSenderName;

    @Autowired
    private JavaMailSender mailSender;
    //发送邮件的模板引擎
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;


    @Override
    public String preview(Map<String, String> params, String templateName) throws Exception {
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
        String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
        return text;
    }

    public void sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    @Override
    public void sendSimpleMail(String to, String subject, String content, String... cc) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setCc(cc);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    public void sendHtmlMail(String subject, String to, Map<String, Object> params, String templateName) throws Exception {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setFrom(from);
        mimeMessageHelper.setTo(to);//发送给谁
        mimeMessageHelper.setSubject(subject);//邮件标题
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
        String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
        mimeMessageHelper.setText(text, true);
        mailSender.send(mimeMessage);

    }


    public void sendHtmlMail(String subject, Map<String, Object> params, String templateName, String to, String... cc) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        //邮件发送人=网站名称+<邮箱>
        helper.setFrom(MimeUtility.encodeWord(systemSenderName) + "<" + from + ">");
        helper.setTo(to);
        helper.setCc(cc);
        helper.setSubject(subject);
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
        helper.setText(content, true);
        mailSender.send(message);
    }

    public void sendHtmlMail(String subject, String to, Map<String, Object> params, String templateName, String... cc) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
        helper.setText(content, true);
        helper.setCc(cc);
        mailSender.send(message);
    }

    public void sendAttachmentsMail(String subject, String to, Map<String, Object> params, String templateName, String filePath) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
        helper.setText(content, true);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        helper.addAttachment(fileName, file);
        mailSender.send(message);
    }

    public void sendAttachmentsMail(String subject, String to, Map<String, Object> params, String templateName, String filePath, String... cc) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
        String content = FreeMarkerTemplateUtils.processTemplateIntoString(template, params);
        helper.setText(content, true);
        helper.setCc(cc);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
        helper.addAttachment(fileName, file);
        mailSender.send(message);
    }

    @Override
    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, res);
        mailSender.send(message);
    }

    @Override
    public void sendResourceMail(String to, String subject, String content, String rscPath, String rscId, String... cc) throws MessagingException {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        FileSystemResource res = new FileSystemResource(new File(rscPath));
        helper.addInline(rscId, res);
        mailSender.send(message);
    }

}
